package com.bawei.yangzongcai___chenmei.SlindingMenu;


import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.RatingBar;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bawei.yangzongcai___chenmei.Adpter.QAQadpterActivity2;
import com.bawei.yangzongcai___chenmei.JavaClass.JavaJinxi;
import com.bawei.yangzongcai___chenmei.MVC.BsaeActivity;
import com.bawei.yangzongcai___chenmei.R;
import com.bawei.yangzongcai___chenmei.SQL.SQLyongyong2;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class SlingMenuActivity2 extends BsaeActivity implements OnItemChildClickListener, OnItemChildLongClickListener {

    List<JavaJinxi.ResBean.VerticalBean> list;
    private RecyclerView slingmenuActivity2RV;
    QAQadpterActivity2 qqaq;

    @Override
    public int bandLayout() {
        return R.layout.activity_sling_menu2;
    }

    @Override
    public void initView() {
        slingmenuActivity2RV = findViewById(R.id.slingmenuActivity2_RV);
        slingmenuActivity2RV.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
    }

    @Override
    public void initDate() {

        OkGo.<String>get("http://service.picasso.adesk.com/v1/vertical/category/4e4d610cdf714d2966000000/vertical?limit=30&adult=false&first=1&order=new").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Gson gson = new Gson();
                JavaJinxi jinxi = gson.fromJson(response.body().toString(),JavaJinxi.class);
                list = new ArrayList<>();
                list=jinxi.getRes().getVertical();
                inintof(list);
            }
        });

    }

    public void  inintof(List<JavaJinxi.ResBean.VerticalBean> list)
    {
        if (qqaq==null)
        {
            qqaq  = new QAQadpterActivity2(list);
            qqaq.setOnItemChildLongClickListener(this);
            qqaq.setOnItemChildClickListener(this);
            slingmenuActivity2RV.setAdapter(qqaq);
        }
    }

    @Override
    public boolean onItemChildLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId())
        {
            case R.id.kauiqu_img:
                ToastUtils.showShort("缓存到数据库了");
                SQLyongyong2 yong2 = new SQLyongyong2(this,"chenchen.dp",null,1);
                SQLiteDatabase reabase = yong2.getReadableDatabase();
                reabase.execSQL("insert into chenchen values(?,?)",new Object[]{
                        list.get(position).getId(),
                        list.get(position).getImg()
                });

                break;
        }
        return true;
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId())
        {
            case R.id.kauiqu_img:
                ToastUtils.showShort("点击了一下："+list.get(position).getId());
                break;
        }
    }
}
